<template>
	<view class="register-container">
		<qmy-custom-navbar title="用户注册" :showBack="true"></qmy-custom-navbar>
		
		<view class="form-container">
			<view class="input-group">
				<view class="input-label">手机号</view>
				<input class="input-field" v-model="formData.phone" placeholder="请输入手机号" type="number" maxlength="11" />
			</view>
			
			<view class="input-group">
				<view class="input-label">用户名</view>
				<input class="input-field" v-model="formData.username" placeholder="请输入用户名" />
			</view>
			
			<view class="input-group">
				<view class="input-label">密码</view>
				<input class="input-field" v-model="formData.password" placeholder="请输入密码" type="password" />
			</view>
			
			<view class="input-group">
				<view class="input-label">确认密码</view>
				<input class="input-field" v-model="confirmPassword" placeholder="请再次输入密码" type="password" />
			</view>
			
			<button class="register-btn" @click="register">注册</button>
			
			<view class="login-link" @click="goToLogin">
				已有账号？去登录
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				formData: {
          phone: '',
					username: '',
					password: ''
				},
				confirmPassword: ''
			}
		},
		methods: {
			async register() {
				if (!this.validateForm()) {
					return
				}
				
				try {
					this.$Loading('注册中')
					const result = await this.$auth.register(this.formData)
					this.$Toast('注册成功')
					setTimeout(() => {
						this.goToLogin()
					}, 1500)
				} catch (error) {
					console.error('注册失败:', error)
					this.$Toast(error.message || '注册失败')
				} finally {
					uni.hideLoading()
				}
			},
			
			validateForm() {
				if (!this.formData.phone) {
					this.$Toast('请输入手机号')
					return false
				}
				if (!/^1\d{10}$/.test(this.formData.phone)) {
					this.$Toast('请输入正确的手机号')
					return false
				}
				if (!this.formData.username) {
					this.$Toast('请输入用户名')
					return false
				}
				if (!this.formData.password) {
					this.$Toast('请输入密码')
					return false
				}
				if (this.formData.password.length < 6) {
					this.$Toast('密码长度不能少于6位')
					return false
				}
				if (this.formData.password !== this.confirmPassword) {
					this.$Toast('两次输入的密码不一致')
					return false
				}
				return true
			},
			
			goToLogin() {
				uni.navigateTo({
					url: '/qmy_user/login'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.register-container {
		min-height: 100vh;
		background-color: #f8f8f8;
	}
	
	.form-container {
		padding: 40rpx;
    margin-top: 100rpx;
	}
	
	.input-group {
		margin-bottom: 30rpx;
	}
	
	.input-label {
		font-size: 28rpx;
		color: #333;
		margin-bottom: 10rpx;
	}
	
	.input-field {
		width: 100%;
		height: 80rpx;
		background-color: #fff;
		border-radius: 10rpx;
		padding: 0 20rpx;
		font-size: 28rpx;
		border: 1rpx solid #e0e0e0;
	}
	
	.register-btn {
		width: 100%;
		height: 80rpx;
		background-color: #00ba31;
		color: white;
		border-radius: 10rpx;
		font-size: 32rpx;
		margin-top: 40rpx;
		border: none;
	}
	
	.login-link {
		text-align: center;
		margin-top: 30rpx;
		font-size: 28rpx;
		color: #00ba31;
	}
</style> 